/*
 * COPYRIGHT. ShenZhen iByte Technology Co., Ltd. 2018.
 * ALL RIGHTS RESERVED.
 *
 * No part of this publication may be reproduced, stored in a retrieval system, or transmitted,
 * on any form or by any means, electronic, mechanical, photocopying, recording, 
 * or otherwise, without the prior written permission of ShenZhen iByte Network Technology Co., Ltd.
 *
 * Amendment History:
 * 
 * Date                   By              Description
 * -------------------    -----------     -------------------------------------------
 * Dec 3, 2018    Li.shangzhi         Create the class
 */
package com.github.icloud.elasticsearch.api.search;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import com.github.icloud.elasticsearch.ElasticsearchClientBase;
/**
 * @FileName MultiSearchAPI.java
 * @Description: 在同一API中执行多个搜索请求
 *
 *	官方文档 @see <a href='https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search-scrolling.html'></a>
 *
 * @Date Dec 3, 2018 2:45:46 PM
 * @author Li.shangzhi
 * @version 1.0
 */
public class MultiSearchAPI extends ElasticsearchClientBase{

	@Test
	public void iEsMultiSearch() throws Exception{
		SearchRequestBuilder srb1 = client
				.prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);

		SearchRequestBuilder srb2 = client
				.prepareSearch().setQuery(QueryBuilders.matchQuery("age", "15")).setSize(1);

		MultiSearchResponse sr = client.prepareMultiSearch()
				.add(srb1)
				.add(srb2)
				.get();

		long nbHits = 0;
		for (MultiSearchResponse.Item item : sr.getResponses()){
			SearchResponse response = item.getResponse();
			nbHits += response.getHits().getTotalHits();
			println(response);
		}
		System.out.println("nbHits:" + nbHits);

	}

}
